package com.monitor.service.parser.impl;

import com.monitor.model.ServerStatus;
import com.monitor.service.parser.MetricParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * CPU指标解析器
 * 负责解析CPU使用率信息
 */
public class CpuMetricParser implements MetricParser {
    private static final Logger logger = LoggerFactory.getLogger(CpuMetricParser.class);

    @Override
    public void parse(String commandOutput, ServerStatus status) {
        try {
            // 去除空格并处理可能的百分号
            String cpuUsage = commandOutput.trim();
            if (cpuUsage.endsWith("%")) {
                cpuUsage = cpuUsage.substring(0, cpuUsage.length() - 1);
            }
            status.setCpuUsage(cpuUsage + "%");
        } catch (Exception e) {
            logger.error("解析CPU使用率失败: {}", e.getMessage());
            status.setCpuUsage("解析错误");
        }
    }
} 